//package com.ruoyi.framework.config;
//
//import com.ruoyi.system.domain.SessionBean;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.CloseStatus;
//import org.springframework.web.socket.TextMessage;
//import org.springframework.web.socket.WebSocketSession;
//import org.springframework.web.socket.handler.AbstractWebSocketHandler;
//
//import javax.websocket.Session;
//import java.io.IOException;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.atomic.AtomicInteger;
//
//@Component
//public class MyWsHandler extends AbstractWebSocketHandler {
//    private static Map<String, SessionBean> sessionBeanMap;
//    private static AtomicInteger clientId;
//
//    private StringBuffer stringBuffer = new StringBuffer();
//
//    static {
//        sessionBeanMap = new ConcurrentHashMap<>();
//        clientId =new AtomicInteger(0);
//
//    }
//    @Override
//    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//        super.afterConnectionEstablished(session);
//        int andIncrement = clientId.getAndIncrement();
//        System.out.println("-------------"+andIncrement);
//        SessionBean sessionBean = new SessionBean(session,andIncrement);
//        sessionBeanMap.put(session.getId(),sessionBean);
//        System.out.println("连接之后--"+session.getId());
//        stringBuffer.append(sessionBeanMap.get(session.getId()).getClientId()+":"+"加入了该聊天<br/>");
//        sendMessage(sessionBeanMap);
//
//    }
//
//    @Override
//    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
//        super.handleTextMessage(session, message);
//        System.out.println(message.getPayload());
//        stringBuffer.append(sessionBeanMap.get(session.getId()).getClientId()+":"+message.getPayload()+"<br/>");
//        sendMessage(sessionBeanMap);
//
//    }
//
//    //连接关闭
//    @Override
//    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
//        super.afterConnectionClosed(session, status);
//        Integer clientId1 = sessionBeanMap.get(session.getId()).getClientId();
//        sessionBeanMap.remove(session.getId());
//        stringBuffer.append(clientId1+":离开群聊<br/>");
//        sendMessage(sessionBeanMap);
//
//    }
//
//    @Override
//    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
//        super.handleTransportError(session, exception);
//        if (session.isOpen()){
//            session.close();
//        }
//        sessionBeanMap.remove(session.getId());
//    }
//
////    @Scheduled(fixedRate = 2000)
////    public void SendMsg() throws IOException {
////        for (String s : sessionBeanMap.keySet()) {
////            sessionBeanMap.get(s).getWebSocketSession().sendMessage(new TextMessage("心跳"));
////            System.out.println("key:"+s);
////            System.out.println("ClientId"+ sessionBeanMap.get(s).getClientId());
////        }
////    }
//
//    public  void sendMessage(Map<String, SessionBean> sessionBeanMap){
//        for (String s : sessionBeanMap.keySet()) {
//            try {
//                sessionBeanMap.get(s).getWebSocketSession().sendMessage(new TextMessage(stringBuffer.toString()));
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//        }
//
//    }
//
//
//}
